package cn.xm.org.service.impl;

import cn.xm.basic.constants.Constant;
import cn.xm.basic.service.impl.BaseServiceImpl;
import cn.xm.org.domain.Department;
import cn.xm.org.mapper.DepartmentMapper;
import cn.xm.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.findById(id);
    }

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
        // 获取department的dirpath
        Department parent = department.getParent();
        String dirpath="/";
        // 如果没有选上级部门，路径就是"/"+id
        if (parent == null) {
            dirpath = dirpath + department.getId();
        } else {
            // 有上级部门就是上级部门的parent+"/"+id
            parent = departmentMapper.findById(parent.getId());
            dirpath = parent.getDirPath() + "/" + department.getId();
        }
        // 更新dirpath
        department.setDirPath(dirpath);
        departmentMapper.update(department);
        // 删除缓存
        redisTemplate.delete(Constant.DEPT_TREE_KEY);
    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
        // 删除缓存
        redisTemplate.delete(Constant.DEPT_TREE_KEY);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        departmentMapper.delete(id);
        // 删除缓存
        redisTemplate.delete(Constant.DEPT_TREE_KEY);
    }

//    @Override
//    public PageList<Department> queryByPage(DepartmentQuery departmentQuery) {
//        Long totals = departmentMapper.queryCount(departmentQuery);
//        List<Department> departments = departmentMapper.queryByPage(departmentQuery);
//        PageList<Department> data = new PageList<Department>(totals,departments);
//        return data;
//    }

    @Override
    public void batchDelete(List<Long> ids) {
        super.batchDelete(ids);
        // 删除缓存
        redisTemplate.delete(Constant.DEPT_TREE_KEY);
    }


    @Override
    public List<Department> deptTree() {
        List<Department> depts = departmentMapper.findAll();
        Map<Long, Department> map = depts.stream().collect(Collectors.toMap(Department::getId, a -> a));
        // 创建一个list
        List<Department> result = new ArrayList<>();
        for (Department dept : depts) {
            // 找出最顶级的部门
            if (dept.getParentId() == null) {
                result.add(dept);
            } else {
                // 剩下的是非顶级部门
                // 找到每个部门对应的父级部门
                Department parent = map.get(dept.getParentId());
                // 将子部门添加到父级部门的children里
                parent.getChildren().add(dept);
            }
        }
        return result;
    }

    @Override
    public List<Department> getDeptTree() {
        // 先从redis里面取
        List<Department> departments = (List<Department>)redisTemplate.opsForValue().get(Constant.DEPT_TREE_KEY);
        // 如果redis里面存在
        if (departments != null) {
            return departments;
        } else {
            // 不存在就从数据库查
            List<Department> result = this.deptTree();
            // 把查到的结果放到redis
            redisTemplate.opsForValue().set(Constant.DEPT_TREE_KEY,result);
            return result;
        }
    }
}
